package com.ziiwee.reduce.describe;

import com.ziiwee.reduce.describe.annotations.Label;
import com.ziiwee.reduce.describe.annotations.Type;
import org.springframework.core.ResolvableType;
import org.springframework.util.StringUtils;

import java.lang.reflect.AnnotatedElement;

/**
 * @author ziiwee
 * @data 2020/8/12
 */
public abstract class AbstractDescribe implements Describe {

    public abstract AnnotatedElement getElement();

    public abstract ResolvableType getDefaultType();

    @Override
    public ResolvableType getType() {
//        Util.get(getElement(),Type.class,it->)
        for (Type it : getElement().getAnnotationsByType(Type.class)) {
            if (it.typeArgs().length > 0) {
                return ResolvableType.forClassWithGenerics(it.value(), it.typeArgs());
            }
            return ResolvableType.forClass(it.value());
        }
        return getDefaultType();
    }

    @Override
    public String getLabel() {
        for (Label label : getElement().getAnnotationsByType(Label.class)) {
            if (StringUtils.hasLength(label.value())) {
                return label.value();
            }
        }
        return getName();
    }

    @Override
    public String getDescription() {
        for (Label label : getElement().getAnnotationsByType(Label.class)) {
            if (StringUtils.hasLength(label.desc())) {
                return label.desc();
            }
        }
        return "";
    }
}
